package cn.com.fuhuitong.main.access.ui.fragment

import cn.com.fuhuitong.base.BaseAbstractFragment
import com.jakewharton.rxbinding2.widget.RxTextView
import cn.com.fuhuitong.R
import io.reactivex.Observable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import io.reactivex.functions.Function5
import io.reactivex.functions.Function8
import kotlinx.android.synthetic.main.fragment_access_register.*

class AccessRegisterFragment : BaseAbstractFragment() {

    private val dispose: CompositeDisposable = CompositeDisposable()

    companion object {
        fun newInstance(): AccessRegisterFragment {
            return AccessRegisterFragment()
        }
    }

    override val layoutResId: Int
        get() = R.layout.fragment_access_register

    override fun initView() {

//        val observableInviteesPhone: Observable<CharSequence> =
//            RxTextView.textChanges(edit_register_invitees_phone)
//        val observableInviteesName: Observable<CharSequence> =
//            RxTextView.textChanges(edit_register_invitees_name)

        val observablePhone: Observable<CharSequence> =
            RxTextView.textChanges(edit_register_phone)
                .doOnNext {
                    text_register_code.isEnabled = it.isNotEmpty()
                }
        val observableNickname: Observable<CharSequence> =
            RxTextView.textChanges(edit_register_nickname)
//        val observableCard: Observable<CharSequence> =
//            RxTextView.textChanges(edit_register_card)
        val observablePassword: Observable<CharSequence> =
            RxTextView.textChanges(edit_register_password)
        val observablePasswordSure: Observable<CharSequence> =
            RxTextView.textChanges(edit_register_password_sure)
        val observableCode: Observable<CharSequence> =
            RxTextView.textChanges(edit_register_code)

        val observable: Disposable = Observable.combineLatest(
//            observableInviteesPhone,
//            observableInviteesName,
            observablePhone,
            observableNickname,
//            observableCard,
            observablePassword,
            observablePasswordSure,
            observableCode,
            Function5< CharSequence, CharSequence, CharSequence, CharSequence, CharSequence, Boolean> { t1, t2, t3, t4, t5 ->
                t1.isNotEmpty() && t2.isNotEmpty() && t3.isNotEmpty() && t4.isNotEmpty() && t5.isNotEmpty()
            })
            .subscribe {
                text_register_register.isEnabled = it
            }
        dispose.add(observable)

    }

    override fun onDestroyView() {
        super.onDestroyView()
        if (dispose.size() > 0 && !dispose.isDisposed) {
            dispose.dispose()
        }
    }
}